package org.petstore.web.servlet;

import com.alibaba.fastjson.JSON;
import org.petstore.domain.Account;
import org.petstore.service.AccountService;
import org.petstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UsernameMatchPasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AccountService accountService=new AccountService();
        resp.setContentType("text/json");//以json形式输出
        PrintWriter out=resp.getWriter();
        Result result1=new Result();
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        //用户名与密码均不为空
        if(password==null||password.equals("")){
            result1.setCode(9);
            result1.setMsg("null");
        }else if(password!=null&&!password.equals("")&&username!=null&&!username.equals("")){
            Account account1=accountService.getAccount(username);
            Account account=accountService.getAccount(username,password);
            if(account1!=null&&account==null){
                //用户名或密码错误
                result1.setCode(8);
                result1.setMsg("password error");
            }else if(account1!=null&&account!=null){
                result1.setCode(10);
                result1.setMsg("success");
            }
        }
        String str = JSON.toJSONString(result1);
        out.print(str);
        out.flush();
        out.close();
    }
}
